//
//  LoginView.m
//  test
//
//  Created by 朱圳 on 2017/12/11.
//  Copyright © 2017年 朱圳. All rights reserved.
//

#import "LoginView.h"
#import "Masonry.h"
#import "UITextField+Limit.h"

@interface LoginView()<UITextFieldDelegate>

@property(nonatomic,strong)UIButton *registBtn;
@property(nonatomic,strong)UIButton *loginBtn;
@property(nonatomic,strong)UIButton *forgetBtn;
@property(nonatomic,strong)UITextField *telField;
@property(nonatomic,strong)UITextField *pwdField;
@end
@implementation LoginView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        {
            [self setUI];
        }
    }
    return self;
}
-(void)setUI{
    

    //背景图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    imageView.image = [UIImage imageNamed:@"login-bg"];
    [self addSubview:imageView];

    UIImageView *loginbgimg = [[UIImageView alloc]init];
    loginbgimg.image = [UIImage imageNamed:@"login_bj"];
    //父视图自身问题造成textField无响应
    loginbgimg.userInteractionEnabled = YES;
    [self addSubview:loginbgimg];
    [loginbgimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(280, 220));
        make.top.mas_equalTo((ScreenHeight/2));
        make.centerX.equalTo(self);
    }];
    
    UIImageView *telimg = [[UIImageView alloc]init];
    telimg.image = [UIImage imageNamed:@"login-admin"];
    [self addSubview:telimg];
    [telimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 25));
        [make.left.mas_equalTo(loginbgimg.mas_left) setOffset:30];
        [make.top.mas_equalTo(loginbgimg.mas_top) setOffset:40];
    }];
    
    UITextField *telField = [[UITextField alloc]init];
    telField.font = [UIFont systemFontOfSize:14];
    telField.placeholder = @"请输入手机号码";
    [telField setLimitTextLength:11];
    telField.keyboardType = UIKeyboardTypeNumberPad;
    telField.clearButtonMode = UITextFieldViewModeAlways;
    telField.delegate = self;
 
    [self addSubview:telField];
    [telField mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.left.mas_equalTo(telimg.mas_right) setOffset:10];
        [make.bottom.mas_equalTo(telimg.mas_bottom) setOffset:10];
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(200);
    }];
    self.telField = telField;
    
    UILabel *x1label = [[UILabel alloc]init];
    x1label.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:x1label];
    [x1label mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.left.mas_equalTo(loginbgimg.mas_left) setOffset:20];
        [make.top.mas_equalTo(telimg.mas_bottom) setOffset:5];
        make.height.mas_equalTo(1);
        [make.right.mas_equalTo(loginbgimg.mas_right) setOffset:-20];
    }];
    
    UIImageView *pwdimg = [[UIImageView alloc]init];
    pwdimg.image = [UIImage imageNamed:@"login-psw"];
    [self addSubview:pwdimg];
    [pwdimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 25));
        [make.left.mas_equalTo(loginbgimg.mas_left) setOffset:30];
        [make.top.mas_equalTo(x1label.mas_bottom) setOffset:20];
    }];
    
    UITextField *pwdField = [[UITextField alloc]init];
    pwdField.font = [UIFont systemFontOfSize:14];
    [pwdField setLimitTextLength:15];
    pwdField.placeholder = @"请输入密码";
    pwdField.secureTextEntry = YES;
    //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    pwdField.clearButtonMode = UITextFieldViewModeAlways;
    pwdField.delegate = self;
    [self addSubview:pwdField];
    [pwdField mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.left.mas_equalTo(telimg.mas_right) setOffset:10];
        [make.bottom.mas_equalTo(pwdimg.mas_bottom) setOffset:10];
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(200);
    }];
    self.pwdField = pwdField;
    //下划线
    UILabel *x2label = [[UILabel alloc]init];
    x2label.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:x2label];
    [x2label mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.left.mas_equalTo(loginbgimg.mas_left) setOffset:20];
        [make.top.mas_equalTo(pwdimg.mas_bottom) setOffset:5];
        make.height.mas_equalTo(1);
        [make.right.mas_equalTo(loginbgimg.mas_right) setOffset:-20];
    }];
    //登录
    self.loginBtn = [[UIButton alloc]init];
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.loginBtn.backgroundColor = dominantColor;
    [self.loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview: self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.left.mas_equalTo(x2label.mas_left) setOffset:2];
        make.height.mas_equalTo(40);
        [make.right.mas_equalTo(x2label.mas_right) setOffset:-2];
      make.centerX.equalTo(self);
      [make.top.mas_equalTo(x2label.mas_bottom) setOffset:30];
    }];
    
    self.telField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"rememberTel"];
    self.pwdField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"rememberpassword"];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.telField resignFirstResponder];
    [self.pwdField resignFirstResponder];
}
//登录
-(void)login:(UIButton *)sender{
    if (_delegate) {
        if ([_delegate respondsToSelector:@selector(loginButton:withTel:andPassword:)]) {
            [_delegate loginButton:sender withTel:self.telField.text andPassword:self.pwdField.text];
        }
    }
}
//注册
-(void)regist:(UIButton *)sender{
    if (_delegate) {
        if ([_delegate respondsToSelector:@selector(registButton:)]) {
            [_delegate registButton:sender];
        }
    }
}
//忘记密码
-(void)forget:(UIButton *)sender{
    if (_delegate) {
        if ([_delegate respondsToSelector:@selector(forgetButton:)]) {
            [_delegate forgetButton:sender];
        }
    }
}
@end
